package web_hr;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class hr_update
 */
@WebServlet("/hr_update")
public class hr_update extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public hr_update() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取员工的更新信息
		String id = request.getParameter("id");
		System.out.println(id);
		String bsalary = request.getParameter("basic_salary");
		String fname = request.getParameter("fname");
		String lname = request.getParameter("lname");
		String age = request.getParameter("age");
		String role = request.getParameter("role");
		
		//对要更新的信息进行验证
		boolean flag = true;
		String str = "\\w{1,20}";
		String bs = "\\d+\\.?\\d*";
		if((!fname.matches(str))||(!lname.matches(str))||(!bsalary.matches(bs))||(role==null)){
			flag=false;
		}
		
		//将更新信息插入数据库
		Model m=new Model("hr_employee");
		if(flag == true){
			String query = "update hr_employee set PRIMARY_SALARY='"+bsalary+"',fname='"+fname+"',lname='"+lname+"',age='"+age+"',role='"+role+"' where emp_id ="+id;
			int n = m.eUpdate(query);
			 if(n==1){
				  request.setAttribute("message","Update sucessfully!");
				  request.setAttribute("url","show.jsp");
				  request.getRequestDispatcher("/redirect.jsp").forward(request,response);
			}else{
				request.setAttribute("message","Update failed!");
				request.setAttribute("url","show.jsp");
				request.getRequestDispatcher("/redirect.jsp").forward(request,response);				
			}
			
		}

	}

}
